#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <net/if.h>

#include <string.h>

static char *get_mac(const char *dev)
{
	char *mac = NULL;
	struct ifreq ifreq;
	int sfd;

	sfd=socket(AF_INET,SOCK_STREAM,0);
	if ( sfd<0 ) {
		return NULL;
	}

	strcpy(ifreq.ifr_name, dev);
	if (ioctl(sfd,SIOCGIFHWADDR, &ifreq)<0){
		close(sfd);
		return NULL;
	}

	mac = (unsigned char *)malloc(32);
	memset(mac, 0, 32);
	sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x",
			(unsigned char)ifreq.ifr_hwaddr.sa_data[0],
			(unsigned char)ifreq.ifr_hwaddr.sa_data[1],
			(unsigned char)ifreq.ifr_hwaddr.sa_data[2],
			(unsigned char)ifreq.ifr_hwaddr.sa_data[3],
			(unsigned char)ifreq.ifr_hwaddr.sa_data[4],
			(unsigned char)ifreq.ifr_hwaddr.sa_data[5]);
	return mac;
}

int main(int argc, char const* argv[])
{
	if (argc != 2) {
		fprintf(stderr, "%s [device]\n", argv[0]);
		return 1;
	}
	char *mac = get_mac(argv[1]);
	printf("Mac[%s]\n", mac);
	return 0;
}
